// "ui";
// ui.layout(
//     <vertical padding="16">
//         <text textSize="16sp" textColor="black" text="请选择地区"/>
//         <input id="name" text="name"/>
//         <button id="ok" text="确定"/>
//     </vertical>
// );

// 弹出对话框让用户输入数字
// var inputNum = dialogs.rawInput("请输入数字");

// 将输入的内容转换为数字
// var num = parseFloat(inputNum);

// 判断输入的内容是否为数字
// if (isNaN(num)) {
//     toast("请输入有效的数字！");
// } else {
//     log("您输入的数字是：" + num);
// }

toast('Hello, AutoX.js');

// exit()


/**
 * @description launcher
 */

try {
    auto.waitFor();
} catch (e) {
    auto();
}


// 导入其他脚本文件
// var network = require('network.js');
// var ui = require('ui.js');
var f = require('f.js');
f.init()
var item = require('itemlist.js');
var post_ = require('post.js');
var addressList = require('address.js');
var change = require('change.js');

var applist = ["com.sankuai.meituan","com.xunmeng.pinduoduo","ctrip.android.view"]
var spot_list = [
    "13812345678",
    "13987654321",
    "13700000000",
    "13612345678",
    "13523456789",
    "13434567890",
    "15012345678",
    "15800000000",
    "15987654321"
  ];

var appinfo = {}
appinfo.address = f.getLocal('address') || 0
appinfo.item = f.getLocal('item') || 0
appinfo.app = f.getLocal('app') || 0
appinfo.date = f.getLocal('date') || f.getCurrentDate()



function update(){
    if ( appinfo.date != f.getCurrentDate() ){
        appinfo.date = f.getCurrentDate()
        f.setLocal('date', appinfo.date)
        appinfo.address = 0
        appinfo.item = 0
        f.setLocal('address', appinfo.address)
        f.setLocal('item', appinfo.item)
    }
}



function mtapp(){

    var bid = applist[0]
    f.active(bid, 5)

    // 打开拼多多
    f.ms({"desc":"超市便利"}, true, 2)
    // 超市便利logo
    f.ms({"id":"iv_main_sugoo_text"}, false, 2)
    // 搜索按钮
    f.ms({"id":"minutes_tv_header_search_button","text":"搜索"}, false, 2)
    // 搜索框
    f.ms({"id":"minutes_vf_search_carousel_text"}, true, 2)

    var itemlist = item.itemlist()
    setText(0, itemlist[appinfo.item])
    // sleep(1000)
    sleep(1000)
    f.ms({"text":"搜索"}, true,5)

    if(f.ms({"text":"找不到你想的"}, false, 2)){
        appinfo.item++
        f.setLocal('item', appinfo.item)
        back()
        sleep(2000)
        back()
        sleep(2000)
        back()
        return
    }

    if(f.ms({"textMatches":"加载数据失败.*"}, false, 2)){
        appinfo.search = 'find_one'
        back()
        sleep(2000)
        back()
        sleep(2000)
        back()
        return
    }

    var price_obj = textMatches('/.*¥space.*/').findOne(2000)
    if( price_obj ){
        log("price1", price_obj.text())
        var price_ = price_obj.text()
        // var title = price_obj.text()
        const price_re = price_obj.text().match(/¥.+?(\d+(?:\.\d+)?)/);
        if ( price_re ){
            var price_ = price_re[0];
            log("price2", price_)
        }

        var shop_ = price_obj.parent().parent().parent().parent().parent().parent().children()
        if(shop_){
            var shop_name_list = shop_[2].children()[1].children()[0].children()[0].children()
            if ( shop_name_list.length > 1 ){
                var shop_name = shop_name_list[1].text()
            }else{
                var shop_name = shop_name_list[0].text()
            }
            log('shop', shop_name)
            var shop = shop_name
        }

        var node = price_obj.parent().parent().parent().children()
        if(node){
            var d = node[0].children()[0];
            log("d", d.text())

            var title = d.text()
            var str = d.text()
            // var str = "鲜活鱼 1000g1000g"
            const match = str.match(/(\d+(?:\.\d+)?)\s*(斤|g|千克)/);
            if (match) {
                var number = parseFloat(match[1]);
                var unit = match[2];
            }
            // console.log([number, unit]); // 输出: [2, "斤"]
            
            if (unit == "斤"){
                var price = price_ / number
            }else if ( unit == "g"){
                var price = price_ / (number/500)
            }else if ( unit == "千克"){
                var price = price_ / (number*2)
            }
            
            var address_List = addressList.address()
            var arr = {
                "product":itemlist[appinfo.item],
                "city":address_List[appinfo.address],
                "price":price_,
                "pdate":appinfo.date,
                'mall':'美团',
                'shop': shop,
                'code':'',
                'spot': spot_list[appinfo.address],
                'title': title,
                'unit': unit,
            }
            var post_data = {
                s:'App.Index.Update_where',                    
                table:'jilingcaiji',
                arr:JSON.stringify(arr),
                where:JSON.stringify({
                    "product":itemlist[appinfo.item],
                    "city":address_List[appinfo.address],
                    "pdate":appinfo.date,
                    'mall':'美团',
                    })
            }

            post_.post_wenfree(post_data)
            post_.post_ybl(arr)

            back();
            sleep(2000)
            back()
            sleep(2000)


            appinfo.item++
            f.setLocal('item', appinfo.item)
            
        }
    }
}



function mtapp_one(){

    var bid = applist[0]
    f.active(bid, 5)

    // 打开拼多多
    f.ms({"desc":"超市便利"}, true, 2)
    // 超市便利logo
    f.ms({"id":"iv_main_sugoo_text"}, false, 2)
    // 搜索按钮
    f.ms({"id":"minutes_tv_header_search_button","text":"搜索"}, false, 2)
    // 搜索框
    f.ms({"text":"生鲜/菜市场"}, true, 2)
    // 店铺
    if (id('poi_card_style_new_poi_name').findOne(200)){
        f.setLocal('shop',id('poi_card_style_new_poi_name').findOne(200).text())
    }
    
    f.ms({"id":"poi_card_style_new_poi_name"}, true, 2)

    sleep(3000)
    f.ms({"textMatches":"搜索店内商品.*"}, true, 2)
    sleep(3000)

    var itemlist = item.itemlist()

    if (f.ms({"text":"热门搜索"}, false, 2)){
        setText(0, itemlist[appinfo.item])
        sleep(1000)
        f.ms({"text":"搜索"}, true,2)
    }

    if(f.ms({"text":"找不到你想的"}, false, 2)){
        appinfo.item++
        f.setLocal('item', appinfo.item)
        back()
        sleep(2000)
        back()
        sleep(2000)
        back()
        return
    }

    var price_obj = textMatches('/.*\¥.*/').depth(16).findOne(2000)
    if( price_obj ){
        log("price1", price_obj.text())
        var price_ = price_obj.text()
        // var title = price_obj.text()
        // const price_re = price_obj.text().match(/¥.+?(\d+(?:\.\d+)?)/);
        // if ( price_re ){
        //     var price_ = price_re[0];
        //     log("price2", price_)
        // }

        var node = price_obj.parent().children()
        if(node){
            var d = node[2];
            log("d", d.text())

            var title = d.text()
            var str = d.text()
            // var str = "鲜活鱼 1000g1000g"
            const match = str.match(/(\d+(?:\.\d+)?)\s*(斤|g|千克|ml|枚|两|瓶|份|罐|桶|克)/);
            if (match) {
                var number = parseFloat(match[1]);
                var unit = match[2];
            }
            // console.log([number, unit]); // 输出: [2, "斤"]
            
            if (unit == "斤"){
                var price = price_ / number
            }else if ( unit == "g"){
                var price = price_ / (number/500)
            }else if ( unit == "千克"){
                var price = price_ / (number*2)
            }
            
            var address_List = addressList.address()

            var arr = {
                "product":itemlist[appinfo.item],
                "city":address_List[appinfo.address],
                "price":price_,
                "pdate":appinfo.date,
                'mall':'美团',
                'shop': f.getLocal('shop') || '小超市',
                'code':'',
                'spot': spot_list[appinfo.address],
                'title': title,
                'unit': unit,
            }
            log('这里要报错2', arr)

            var post_data = {
                s:'App.Index.Update_where',                    
                table:'jilingcaiji',
                arr:JSON.stringify(arr),
                where:JSON.stringify({
                    "product":itemlist[appinfo.item],
                    "city":address_List[appinfo.address],
                    "pdate":appinfo.date,
                    'mall':'美团',
                    })
            }

            post_.post_wenfree(post_data)
            post_.post_ybl(arr)
            appinfo.item++
            f.setLocal('item', appinfo.item)
            
        }
        back()
        sleep(2000)
    }
}



// appinfo.item = 0
// appinfo.address = num
// appinfo.app = 0
// // appinfo.search = 'find_all'
// appinfo.shop = f.getLocal('shop') || ''
// var address_List = addressList.address()

// var bid = applist[0]
// f.active(bid, 5)
// // f.ms({"text":"超市便利"},true,2)
// // sleep(5000)

// change.lcal()
// log("appinfo.item", appinfo.item, "item", item.itemlist().length-1)

// while (appinfo.item < item.itemlist().length){
//     update()
//     if ( appinfo.search == 'find_all'){
//         mtapp()
//     }else{
//         mtapp_one()
//     }
//     back()
//     sleep(1000)
// }
// // 主脚本的其他逻辑代码
// // ...
// back()
// sleep(1000)
// back()
// sleep(1000)
// f.notice(appinfo.address + '采集完成 > ' + address_List[appinfo.address])


//批查打印app参数
function printAll( ways ) {
    if (!ways){
        ways = 'className'
    }
    if (ways == 'className'){
        var all_Info = classNameMatches(/.*/).find();
    }else
    if (ways == 'text'){
        var all_Info = textMatches(/.*/).find();
    }else
    if (ways == 'id'){
        var all_Info = idMatches(/.*/).find();
    }else{
        eval('var all_Info = ' + ways + '.find();')
    }
    
    if ( all_Info ){
        for (var i = 0;i<all_Info.length;i++){
            var d = all_Info[i];
            if ( d.desc() && d.desc().includes('\ufffc') ){
                // log(i,d.id(),d.text(),"desc:"+d.desc(),'"className":"'+d.className()+'"',
                // "clickable->"+d.clickable(),'selected->'+selected(),"depth->"+d.depth(),
                // d.bounds().centerX(),d.bounds().centerY())
                // log("===========", d.desc())

                var texts = d.desc().split('\n')
                texts.forEach(element => {
                    if ( element.includes('¥') ){
                        log("第二个¥符号后的数字:",element);
                    }
                });

                if ( texts[0].length > 5 ){
                    log(">>>>>", texts[0])
                }
               

            }
        }
    }else{
        log('没有找到任何相关的节点')
    }
}

printAll()

// var price_obj = textMatches('/¥space.*/').findOne(2000)

// var node = descMatches('/.*\¥.*/').findOne(2000)


toast("end")


